'use strict';

angular.module('app')
    .controller('zzyPlatformDetailController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "修改平台";
            var id = $state.params.id;
            
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            
            activate(id);
            
            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/zzyPlatform/detail',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                    	$scope.record = result.data;
                        
                    	var logo = $scope.record.platform_logo;
                    	
                    	$scope.detailImgData = [];
    		            var detailArr = [];
                    	if (logo != null && logo != "") {
        		            detailArr.push($scope.record.platform_logo);
        		            $scope.detailImgData.push({
        	                    src:detailArr
        	                })
                    	}
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            
            $scope.submit = function(){
                $scope.loading = true;
                var detailUrlArr = $scope.detailImgData[0];
                if (!detailUrlArr) {
                	$scope.alertModal({'content':'请上传平台LOGO'});
                    return;
                }

                var detailUrl = $scope.detailImgData[0].src;
                $scope.record.platform_logo = detailUrl[0];
                
                var m = $scope.record;
                $scope.isDisabled = true;//提交disabled
                
                if (m) {
                    $.ajax({
        				url : $scope.app.project_name + '/zzyPlatform/update',
        				data: $scope.record
        			}).then(callback);
                }
                
                function callback(result) {
                    if(result.httpCode ==200){//成功
                        $timeout(function(){
                        	$scope.isDisabled = false;
                            $state.go('main.zzy.zzyPlatform.list');
                        },2000);
                    }else{
                    	$scope.isDisabled = false;
                    	$scope.alertModal({"content":"保存失败"});
                    }
                }
            };

    } ]);